<html>
	<head>
		<meta charset="UTF-8">
		<title>get_tree</title>
	</head>
	<body>
		<p>查看控制台</p>
	</body>
<script type="text/javascript">
let arr_node = [
	{id: 1, pid: 0, name: '人员架构', level: 1, type: 1,},
	{id: 2, pid: 0, name: '员工互评', level: 1, type: 1,},
	{id: 3, pid: 1, name: '导入员工', level: 2, type: 2,},
	{id: 4, pid: 1, name: '部门架构图', level: 2, type: 2,},
	{id: 5, pid: 1, name: '员工管理', level: 2, type: 2,},
	{id: 6, pid: 2, name: '评分', level: 2, type: 2,},
	{id: 7, pid: 2, name: '我的得分', level: 2, type: 2,},
	{id: 8, pid: 2, name: '所有评分', level: 2, type: 2,},
	{id: 9, pid: 3, name: '上传权限', level: 3, type: 3,},
	{id: 10, pid: 3, name: '提交权限', level: 3, type: 3,},
	{id: 11, pid: 8, name: '筛选查询', level: 3, type: 3,},
	{id: 12, pid: 8, name: '导出', level: 3, type: 3,},
];
//网上找的
function get_tree_com(a, idStr, pidStr, chindrenStr){
	let r = [], hash = {}, id = idStr, pid = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length;
	for(; i < len; i++){
		hash[a[i][id]] = a[i];
	}
	for(; j < len; j++){
		let aVal = a[j], hashVP = hash[aVal[pid]];		//当前对象&pid对象
		if(hashVP){
			!hashVP[children] && (hashVP[children] = []);
			hashVP[children].push(aVal);
		}else{
			r.push(aVal);
		}
	}
	return r;
}
//自己写的
function get_tree(arr){
	let ret = [], hash = {}, i = 0, j = 0, len = arr.length;
	for(; i < len; i++){
		hash[arr[i]['id']] = arr[i];
	}
	for(; j < len; j++){
		let arr_val = arr[j], p_val = hash[arr_val['pid']]
		if(p_val){
			!p_val['children'] && (p_val['children'] = []);
			p_val['children'].push(arr_val);
		}else{
			ret.push(arr_val);
		}
	}
	return ret;
}

// var tree_node = get_tree_com(arr_node, 'id', 'pid', 'children');
var tree_node = get_tree(arr_node);
console.log(tree_node);

</script>
</html>